/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wf.ui;

import bc.dsl.PageDSL;
import bc.dsl.SwingDSL;
import bc.swing.pfrm.Model;
import bc.swing.pfrm.ano.Action;
import bc.swing.pfrm.ano.DataExtractor;
import bc.swing.pfrm.ano.OnEvent;
import bc.swing.pfrm.ano.PageDef;
import bc.swing.pfrm.ano.Param;
import bc.swing.pfrm.viewtypes.ParamType;
import java.util.Arrays;
import java.util.List;
import wf.model.Counter;
import wf.model.PLC;
import wf.model.User;
import wf.model.Valve;
import wf.ui.cmps.ManagmentTablePV;
import wf.ui.layouts.MainLayout;
import wf.ui.layouts.ManagmentPortletLayout;
import wf.units.ApplicationUnit;
import wf.units.ModificationUnit;
import wf.units.DatabaseUnit;

/**
 *
 * @author bennyl
 */
@PageDef(name = "Administrator Console", icon = "managment-logo128", layout = MainLayout.class)
public class AdminConsole extends Model {

    private UserConsole userc;
    private ValveConsole valvec;
    private CounterConsole counterc;
    private PLCConsole plcc;

    public AdminConsole() {
        userc = new UserConsole();
        valvec = new ValveConsole();
        counterc = new CounterConsole();
        plcc = new PLCConsole();
    }

    @Param(name = "Active UNAME", role = MainLayout.USER_NAME_ROLE, type = ParamType.LABEL)
    public String getActiveUserName() {
        return ApplicationUnit.UNIT.getRegisteredUser().getName();
    }

    @Action(name = "Close", icon = "close16")
    public void handleClose() {
        getPage().disposeView();
    }

    @OnEvent(name = DatabaseUnit.DATABASE_CHANGED_EVENT, extract = {DatabaseUnit.DBCE_DATA_CLASS})
    void onDataInsertionModification(Class type) {
        if (type == User.class) {
            userc.update();
        } else if (type == Valve.class) {
            valvec.update();
        } else if (type == Counter.class) {
            counterc.update();
        } else if (type == PLC.class) {
            plcc.update();
        }
    }

    @Param(name = "all", type = ParamType.PORTLETS, role = MainLayout.VIEWS_ROLE)
    public List<Model> getModels() {
        return Arrays.<Model>asList(userc, plcc, valvec, counterc);
    }

    public static void main(String[] args) {
        SwingDSL.configureUI();
        DatabaseUnit.UNIT.connect();
        PageDSL.showInFrame(new AdminConsole());
    }

    @PageDef(name = "Users Managment", icon = "users32", layout = ManagmentPortletLayout.class)
    public static class UserConsole extends Model {

        protected List<User> data;

        public UserConsole() {
            data = DatabaseUnit.UNIT.selectAllUsers();
        }

        @Param(name = "Data", customView = ManagmentTablePV.class, role = ManagmentPortletLayout.CENTER_ROLE)
        public List<User> getData() {
            return data;
        }

        @DataExtractor(columns = {"User Name", "Role"}, param = "Data")
        public Object extractUser(String c, User from) {
            if (c.equals("User Name")) {
                return from.getName();
            }
            return from.getRole();
        }

        @Action(name = "Add New User", icon = "add16")
        void handleAddNew() {
            User c = ModificationUnit.UNIT.invokeUserCreation();
            if (c != null) {
                update();
            }
        }

        private void update() {
            data = DatabaseUnit.UNIT.selectAllUsers();
            syncToView();
        }

        @Action(name = "Edit", icon = "edit16", forParam = "Data")
        void handleEdit() {
            User c = (User) getPage().getParam("Data").getSelectedItem();
            if (ModificationUnit.UNIT.invokeUserModification(c)) {
                update();
            }
        }

        @Action(name = "Delete", icon = "delete16", forParam = "Data")
        void handleDelete() {
            User c = (User) getPage().getParam("Data").getSelectedItem();
            if (ModificationUnit.UNIT.invokeDeletion(c)) {
                update();
            }
        }
    }

    @PageDef(name = "Valve Managment", icon = "valve-man32", layout = ManagmentPortletLayout.class)
    public static class ValveConsole extends Model {

        protected List<Valve> data;

        public ValveConsole() {
            data = DatabaseUnit.UNIT.selectAllValves();
        }

        @Param(name = "Data", customView = ManagmentTablePV.class, role = ManagmentPortletLayout.CENTER_ROLE)
        public List<Valve> getData() {
            return data;
        }

        @DataExtractor(columns = {"Name", "PLC IP", "Port"}, param = "Data")
        public Object extractUser(String c, Valve from) {
            if (c.equals("Name")) {
                return from.getName();
            } else if (c.equals("PLC IP")) {
                return from.getPlcIp();
            }
            return from.getPortNum();
        }

        @Action(name = "Add New Valve", icon = "add16")
        void handleAddNew() {
            Valve c = ModificationUnit.UNIT.invokeValveCreation();
            if (c != null) {
                update();
            }
        }

        private void update() {
            //                DatabaseUnit.UNIT.insertToDatabase(c);
            data = DatabaseUnit.UNIT.selectAllValves();
            syncToView();
        }

        @Action(name = "Edit", icon = "edit16", forParam = "Data")
        void handleEdit() {
            Valve c = (Valve) getPage().getParam("Data").getSelectedItem();
            if (ModificationUnit.UNIT.invokeValveModification(c)) {
                update();
            }
        }

        @Action(name = "Delete", icon = "delete16", forParam = "Data")
        void handleDelete() {
            Valve c = (Valve) getPage().getParam("Data").getSelectedItem();
            if (ModificationUnit.UNIT.invokeDeletion(c)) {
                update();
            }
        }
    }

    @PageDef(name = "Counter Managment", icon = "counter32", layout = ManagmentPortletLayout.class)
    public static class CounterConsole extends Model {

        protected List<Counter> data;

        public CounterConsole() {
            data = DatabaseUnit.UNIT.selectAllCounters();
        }

        @Param(name = "Data", customView = ManagmentTablePV.class, role = ManagmentPortletLayout.CENTER_ROLE)
        public List<Counter> getData() {
            return data;
        }

        @DataExtractor(columns = {"Name", "PLC IP", "Port"}, param = "Data")
        public Object extractUser(String c, Counter from) {
            if (c.equals("Name")) {
                return from.getName();
            } else if (c.equals("PLC IP")) {
                return from.getPlcIP();
            }
            return from.getPort();
        }

        @Action(name = "Add New Counter", icon = "add16")
        void handleAddNew() {
            Counter c = ModificationUnit.UNIT.invokeCounterCreation();
            if (c != null) {
                update();
            }
        }

        private void update() {
            data = DatabaseUnit.UNIT.selectAllCounters();
            syncToView();
        }

        @Action(name = "Edit", icon = "edit16", forParam = "Data")
        void handleEdit() {
            Counter c = (Counter) getPage().getParam("Data").getSelectedItem();
            if (ModificationUnit.UNIT.invokeCounterModification(c)) {
                update();
            }
        }

        @Action(name = "Delete", icon = "delete16", forParam = "Data")
        void handleDelete() {
            Counter c = (Counter) getPage().getParam("Data").getSelectedItem();
            if (ModificationUnit.UNIT.invokeDeletion(c)) {
                update();
            }
        }
    }

    @PageDef(name = "PLC Managment", icon = "PLC32", layout = ManagmentPortletLayout.class)
    public static class PLCConsole extends Model {

        protected List<PLC> data;

        public PLCConsole() {
            data = DatabaseUnit.UNIT.selectAllPLCs();
        }

        @Param(name = "Data", customView = ManagmentTablePV.class, role = ManagmentPortletLayout.CENTER_ROLE)
        public List<PLC> getData() {
            return data;
        }

        @DataExtractor(columns = {"Name", "IP"}, param = "Data")
        public Object extractUser(String c, PLC from) {
            if (c.equals("Name")) {
                return from.getName();
            } else {
                return from.getIp();
            }
        }

        @Action(name = "Add New PLC", icon = "add16")
        void handleAddNew() {
            PLC c = ModificationUnit.UNIT.invokePLCCreation();
            if (c != null) {
                update();
            }
        }

        private void update() {
            data = DatabaseUnit.UNIT.selectAllPLCs();
            syncToView();
        }

        @Action(name = "Edit", icon = "edit16", forParam = "Data")
        void handleEdit() {
            PLC c = (PLC) getPage().getParam("Data").getSelectedItem();
            if (ModificationUnit.UNIT.invokePLCModification(c)) {
                update();
            }
        }
        
        @Action(name = "Delete", icon = "delete16", forParam = "Data")
        void handleDelete() {
            PLC c = (PLC) getPage().getParam("Data").getSelectedItem();
            if (ModificationUnit.UNIT.invokeDeletion(c)) {
                update();
            }
        }
    }
}
